﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Vision.AlgBase
{
    /// <summary>
    /// UcServiceTree.xaml 的交互逻辑
    /// </summary>
    public partial class UcServiceTree : UserControl
    {
        public UcServiceTree()
        {
            UcServiceInit();
            InitializeComponent();
        }

        public ObservableCollection<ServiceMetaData> ToolGroup
        {
            get { return toolGroup; }
            set { toolGroup = value; }
        }
        private ObservableCollection<ServiceMetaData> toolGroup = new ObservableCollection<ServiceMetaData>();

        private void UcServiceInit()
        {
            if (!AddInContainer.Instance.IsInit) AddInContainer.Instance.LoadAddIn();
            IEnumerable<Lazy<IAlg, IServiceData>> metas = AddInContainer.Instance.Tools;
            if (metas == null || metas.Count() == 0)
                return;
            foreach (var item in metas)
            {  
                ServiceMetaData metaData = new ServiceMetaData(item.Value)
                {
                    SeriveName = item.Metadata.SeriveName,
                    Descri = item.Metadata.Descri            
                };
                toolGroup.Add(metaData);
            }
        }

        private void treeService_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            base.OnPreviewMouseDown(e);
            dragStartPoint = new Point?(e.GetPosition(this));
        }

        private void treeService_MouseMove(object sender, MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (e.LeftButton != MouseButtonState.Pressed)
            {
                this.dragStartPoint = null;
                return;
            }
            if (this.dragStartPoint.HasValue)
            {
                UIElement element = e.OriginalSource as UIElement;
                ServiceMetaData nearItem = GetNearTreeItem(element);
                if (nearItem != null)
                {
                    DragDrop.DoDragDrop(this, nearItem.Clone(), DragDropEffects.Copy);
                }
                e.Handled = true;
            }
        }

        private ServiceMetaData GetNearTreeItem(UIElement inputelement)
        {            
            TreeViewItem selectedItem = inputelement as TreeViewItem;
            while (inputelement != null && selectedItem == null)
            {
                inputelement = VisualTreeHelper.GetParent(inputelement) as UIElement;
                selectedItem = inputelement as TreeViewItem;
            }
            if (selectedItem == null) return null;
            return selectedItem.DataContext as ServiceMetaData;
        }

        private Point? dragStartPoint = null;
    }

}
